"""列表"""
import sys

list1 = [1, 3, 5, 7]
print('list1 = ', list1, end='\n')
list2 = ['hello'] * 3
# 重复列表元素
print('list2 = ', list2, end='\n')
list3 = ['hello', 'world'] * 3
print('list3 = ', list3, end='\n')
# 计算列表长度
print('the length of list1 is ', len(list1), end='\n')
# 获取下标为 0 的值
print('list1[0] = ', list1[0], end='\n')
# print('list[4] = ', list1[4], end='\n')
print('list1[-1] = ', list1[-1], end='\n')
# print('list1[-5] = ', list1[-5], end='\n')
# 循环获取 key 和 value
for k, v in enumerate(list1):
    print('%d => %d' % (k, v), end='\n')

list1 = [1, 3, 5, 7, 100]
# 添加元素
# list1.append(200)
# 同时添加索引和元素
list1.insert(1, 400)
print('list1 = ', list1, end='\n')
# 合并两个列表
list1.extend([500, 600])
print('list1 = ', list1, end='\n')
list1 += [700, 800]
print('list1 = ', list1, end='\n')

# 删除存在的元素
list1.remove(3)
print('list1 = ', list1, end='\n')
# 删除不存在的元素
# list1.remove(2)
# print('list1 = ', list1, end='\n')

# 从指定位置删除元素
list1.pop(0)
print('list1 = ', list1, end='\n')
# 清空列表
list1.clear()
print('list1 = ', list1, end='\n')

"""列表的切片操作"""

fruits = ['grape', 'apple', 'strawberry', 'waxberry']
fruits += ['pitaya', 'pear', 'mango']
print('fruits = ', fruits, end='\n')
fruits1 = fruits[1:4]
print('fruits1 = ', fruits1, end='\n')
fruits2 = fruits[-3:-1]
print('fruits2 = ', fruits2, end='\n')
fruits3 = fruits[::-1]
print('fruits3 = ', fruits3, end='\n')
# 相当于 fruits[-3:0:-5]
fruits4 = fruits[-3::-5]
print('fruits4 = ', fruits4, end='\n')
# 给列表排序
fruits5 = sorted(fruits)
print('fruits5 = ', fruits5, end='\n')
print('fruits = ', fruits, end='\n')
# 给列表反向排序
fruits6 = sorted(fruits, reverse=True)
print('fruits6 = ', fruits6, end='\n')
# 给列表按照元素长度倒序，
fruits7 = sorted(fruits, key=len, reverse=True)
print('fruits7 = ', fruits7, end='\n')
# 给原列表倒序
fruits.sort(reverse=True)
print('fruits = ', fruits, end='\n')

"""生成式"""

# for 前面的 x 是个表达式，可以是 x * x 或 x + x
# for 后面的 x 是 range(1, 10) 里的每个元素
f = [x for x in range(1, 10)]
print('f = ', f, end='\n')
f = [x + y for x in 'ABC' for y in '123']
print('f = ', f, end='\n')
# 创建列表容器
f = [x ** 2 for x in range(1, 1000)]
print(sys.getsizeof(f), end='\n')
# 创建不占用空间的生成器对象
# 通过生成器可以获取到数据但它不占用额外的空间存储数据
# 每次需要数据的时候就通过内部的运算得到数据(需要花费额外的时间)
f = (x ** 2 for x in range(1, 1000))
print(sys.getsizeof(f), end='\n')


def fib(num):
    m, n = 0, 1
    for i in range(num):
        m, n = n, m + n
        yield m


def main():
    for vv in fib(20):
        print('v = ', vv, end='\n')


if __name__ == '__main__':
    main()
